package cn.tedu.baking.controller;

import cn.tedu.baking.base.response.JsonResult;
import cn.tedu.baking.pojo.dto.ContentDTO;
import cn.tedu.baking.pojo.dto.ContentIndexQueryDTO;
import cn.tedu.baking.pojo.dto.ContentManagementQueryDTO;
import cn.tedu.baking.pojo.vo.ContentDetailVO;
import cn.tedu.baking.pojo.vo.ContentIndexVO;
import cn.tedu.baking.pojo.vo.ContentManagementVO;
import cn.tedu.baking.pojo.vo.ContentUpdateVO;
import cn.tedu.baking.service.ContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/contents")
public class ContentController {
    @Autowired
    private ContentService contentService;

    @PostMapping("add-new")
    public JsonResult addNew(ContentDTO contentDTO) {
        log.debug("新增稿件 contentDTO ={}" ,contentDTO);
         contentService.addNew(contentDTO);
         return JsonResult.ok();
    }
    @GetMapping("management")
    public JsonResult management(ContentManagementQueryDTO contentManagementQueryDTO) {
        log.debug("稿件管理查询 contentManagementQueryDTO ={}" ,contentManagementQueryDTO);
        List<ContentManagementVO> list =contentService.listForManagement(contentManagementQueryDTO);
        return JsonResult.ok(list);
    }
    @PostMapping("{id}/delete")
    public JsonResult deleteById(@PathVariable Long id) {
        log.debug("删除稿件 id={} ",id);
        contentService.deleteById(id);
        return JsonResult.ok();
    }
    @GetMapping("{id}/update")
    public JsonResult getDetailForUpdate(@PathVariable Long id) {
        log.debug("编辑稿件 id={} ",id);
        ContentUpdateVO contentUpdateVO = contentService.getDetailForUpdate(id);
        return JsonResult.ok(contentUpdateVO);
    }

    @GetMapping("index")
    public JsonResult listForIndex(ContentIndexQueryDTO contentIndexQueryDTO){
        log.debug("首页查询 ContentIndexQueryDTO ={}" ,contentIndexQueryDTO);
        List<ContentIndexVO>  list= contentService.listForIndex(contentIndexQueryDTO);
        return JsonResult.ok(list);
    }

    //查询详情页文章数据
    @GetMapping("{id}/detail")
    public JsonResult getDetailById(@PathVariable Long id) {
        ContentDetailVO contentDetailVO =contentService.getDetailById(id);
        return JsonResult.ok(contentDetailVO);
    }
}
